package com.wyl.mine.setting

import android.text.Html
import android.text.method.ScrollingMovementMethod
import android.view.View
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.MineServiceProvision
import com.wyl.base.MineServiceProvision2
import com.wyl.libbase.base.NormalActivity
import com.wyl.mine.R
import kotlinx.android.synthetic.main.service_activity.*

class ServiceActivity : NormalActivity() {
    override fun getLayoutId(): Int = R.layout.service_activity

    override fun initView() {
        titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        textview.movementMethod = ScrollingMovementMethod.getInstance()
    }

    override fun loadData() {
        OkGo.post<BaseResponse<Map<String, String>>>(MineServiceProvision)
            .converter(object : JsonConvert<BaseResponse<Map<String, String>>>() {})
            .adapt(ObservableBody<BaseResponse<Map<String, String>>>())
            .doOnSubscribe { showLoadingDialog() }
            .doFinally { dissmissLoadingDialog() }
            .flatMap {
                val map = it.data
                textview.text = map["title"]
                OkGo.post<BaseResponse<Map<String, String>>>(MineServiceProvision2)
                    .params("id", map["title"] + "\n")
                    .converter(object : JsonConvert<BaseResponse<Map<String, String>>>() {})
                    .adapt(ObservableBody<BaseResponse<Map<String, String>>>())
            }
            .subscribe({
                if (it.code == 0) {
                    textview.append(Html.fromHtml(it.data["content"]))
                }
            }, {
                onError(it)
            }).toDisposables(disposables)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt_close -> onBackPressed()
            else -> {
            }
        }
    }
}